import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { RentalList } from '@app/store/dict';
import { ISystemRole } from '@model/system';
import { AuthService } from '@service/auth.service';
import { SystemService } from '@service/system.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemRoleEditComponent } from './system-role-edit/system-role-edit.component';

@Component({
	selector: 'app-system-role',
	templateUrl: './system-role.component.html',
	styleUrls: ['./system-role.component.less']
})
export class SystemRoleComponent extends AbsFormUtil implements OnInit {


	rentalList = RentalList;
	nzLoading = false;
	data = [];
	constructor(
		private systemService: SystemService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private nzModalService: NzModalService,
		private authService: AuthService

	) {
		super();
		this.authList = this.authService.getAuth();
	}


	ngOnInit() {
		this.buildForm();
		this.onSearch();
		this.page.limit = 1000;
	}
	buildForm() {
		this.validateForm = this.fb.group({
			roleName: []
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		if (isPageInit) {
			this.page.page = 1;
		}
		const params = { ...this.validateForm.value, ...this.page };
		const result = await this.systemService.getRoleList(params);
		this.nzLoading = false;
		this.data = result.list || [];
		this.page.total = result.list?.length;

	}


	pageChange() {
		console.log(this.page);
		// this.onSearch();
	}

	edit(item) {

	}

	async del(item: ISystemRole) {
		await this.systemService.deleteUser(item.id);
		this.nzMessageService.success('删除成功');
		this.onSearch(true);
	}


	add(item?: ISystemRole) {
		console.log(item);
		this.nzModalService.create({
			nzTitle: item ? '编辑角色' : '新增角色',
			nzContent: SystemRoleEditComponent,
			nzBodyStyle: {
				padding: '10px 20px'
			},
			nzWidth: '70%',
			nzComponentParams: {
				id: item?.id
			},
			nzFooter: [
				{
					label: '确定',
					type: 'primary',
					onClick: (componentInstance) => {
						(async () => {
							const resForm = componentInstance.onSubmit();
							if (resForm) {
								const id = componentInstance.id || ''
								resForm.id = id;
								const result = await this.systemService.saveRole(resForm);
								// if (result.status !== 0) {
								// 	this.nzMessageService.error(result.msg || '保存失败');
								// 	return;
								// }
								componentInstance.destroyModal();
								this.onSearch();
								this.nzMessageService.success('保存成功');
							}
						})();
					}
				},
				{
					label: '取消',
					onClick: componentInstance => {
						componentInstance.destroyModal();
					}
				}
			]
		})
	}


}
